package com.zhou.common.operator.log.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public SpringContextUtil() {
    }

    public static List getBeanList(Class beanClz) {
        Map beanMap = applicationContext.getBeansOfType(beanClz);
        return beanMap != null && !beanMap.isEmpty() ? new ArrayList(beanMap.values()) : new ArrayList();
    }

    public static Object getBean(Class beanClz) {
        return applicationContext.getBean(beanClz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
}

